﻿using QRCoder;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QRCodeCreator
{
    public partial class Form_QRCode : Form
    {
    
        public Form_QRCode()
        {
            InitializeComponent();
        }
        OpenFileDialog ofd = new OpenFileDialog();

        private void pictureBox_img_Click(object sender, EventArgs e)
        {          
            ofd.Title = "选择logo图片";
            var r = ofd.ShowDialog();
            ofd.Filter = "(*.jpg)|*.jpg|(*.png)|*.png";
            if (r == DialogResult.OK)
            {
                this.pictureBox_img.Image = Image.FromFile(ofd.FileName);
            }
           
        }

        private void button_create_Click(object sender, EventArgs e)
        {
            string content = this.textBox_content.Text;
            Bitmap icon = this.pictureBox_img.Image as Bitmap;
            var has_border = this.checkBox_has_border.Checked;

            var index= this.comboBox_level.SelectedIndex;
            var level = QRCodeGenerator.ECCLevel.H;
            switch (index)
            {
                case 0:
                    level= QRCodeGenerator.ECCLevel.H;
                    break;
                case 1:
                    level = QRCodeGenerator.ECCLevel.Q;
                    break;
                case 2:
                    level = QRCodeGenerator.ECCLevel.M;
                    break;
                case 3:
                    level = QRCodeGenerator.ECCLevel.L;
                    break;
                default:
                    level = QRCodeGenerator.ECCLevel.H;
                    break;
            }
            var pix = this.trackBar_pix.Value;
            var border = this.trackBar_img_border.Value;
            var icon_per = this.trackBar_per_img.Value;
            var qr = QRCodeHelper.Create(content, pix, this.pictureBox_font_color.BackColor, this.pictureBox_back_color.BackColor, icon, icon_per, border, has_border,level);
            this.pictureBox_qrcode.Image = qr;            

        }
     
        private void textBox_front_color_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void pictureBox_font_color_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            var r = cd.ShowDialog();
            if (r == DialogResult.OK)
            {
                this.pictureBox_font_color.BackColor = cd.Color;
                cd.Dispose();
            }
        }

        private void pictureBox_back_color_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            var r = cd.ShowDialog();
            if (r == DialogResult.OK)
            {
                this.pictureBox_back_color.BackColor = cd.Color;
                cd.Dispose();
            }
        }


        private void button_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.OverwritePrompt = true;
            sfd.DefaultExt = ".jpg";
            sfd.Filter = "(*.jpg)|*.jpg|(*.png)|*.png";
            if (sfd.ShowDialog()==DialogResult.OK)
            {
                string path = sfd.FileName;
                pictureBox_qrcode.Image.Save(path+".jpg");
            }
        }

        private void trackBar_per_img_Scroll(object sender, EventArgs e)
        {
            var per = this.trackBar_per_img.Value;
            this.label_icon_per.Text = "logo占比（" + per + "%）";
        }
    }
}
   